package com.sz.biz.common.customer.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.common.customer.entity.CusCustomerContacts;
import com.sz.biz.common.customer.service.CustomerContactsService;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xutao on 17/4/11.
 */
@Service("CustomerContactsService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CustomerContactsServiceImpl extends AbstractService implements CustomerContactsService {

    @Override
    public int save(CusCustomerContacts entry) {

        checkDataValid(entry);

        dao.save(getSqlName("insertSelective"), entry);

        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_CUSTOMER_CONTACTS", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.CUS, ModuleNames.CUS_CUSTOMER, UserActions.ADD, "新增客户联系人信息", dataBackup);

        return entry.getId();
    }

    @Override
    public int delete(List<Integer> ids) {

        if (ids != null && ids.size() > 0) {

            dao.update(getSqlName("logicDeleteByIds"), ids);
        }

        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_CUSTOMER_CONTACTS", ids);
        PrincipalLogUtils.addOperationLog(ModuleNames.CUS, ModuleNames.CUS_CUSTOMER, UserActions.DELETE, "批量删除客户联系人信息", dataBackup);

        return 0;
    }

    @Override
    public void update(CusCustomerContacts entry) {

        checkDataValid(entry);

        dao.update(getSqlName("updateById"), entry);

        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_CUSTOMER_CONTACTS", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.CUS, ModuleNames.CUS_CUSTOMER, UserActions.UPDATE, "更新客户联系人信息", dataBackup);

    }

    @Override
    public QResultDto find(String q, String sortName, boolean isAsc, int pageIndex, int pageSize, int refCustomerId) {

        ParamData pd = this.convertQueryParams(new QueryParams(pageSize, pageIndex, q, sortName, isAsc, null).put("refCustomerId", refCustomerId));

        List<CusCustomerContacts> resList = dao.findForList(getSqlName("findByParamListPage"), pd, CusCustomerContacts.class);

        QResultDto qResultDto = new QResultDto();

        qResultDto.setPagination(pd.getPagination());
        qResultDto.setData(resList);

        return qResultDto;
    }

    @Override
    public List<CusCustomerContacts> findByCustomerId(Integer refCustomerId) {

        List<CusCustomerContacts> cusCustomerContactsList = dao.findForList(getSqlName("findByCustomerId"), refCustomerId, CusCustomerContacts.class);

        return cusCustomerContactsList;
    }

    @Override
    public CusCustomerContacts findById(Integer id) {

        CusCustomerContacts cusCustomerContacts = (CusCustomerContacts) dao.findForObject(getSqlName("selectByPrimaryKey"), id);

        return cusCustomerContacts;
    }

    private void checkDataValid(CusCustomerContacts cusCustomerContacts) {

        if (StringUtils.isEmpty(cusCustomerContacts.getName())) {

            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "name");
        }

        if (StringUtils.isEmpty(cusCustomerContacts.getDepartment())) {

            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "department");
        }

        if (StringUtils.isEmpty(cusCustomerContacts.getFunction())) {

            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "function");
        }

        if (StringUtils.isEmpty(cusCustomerContacts.getMobilePhone())) {

            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "mobilephoneNo");
        }

        if (StringUtils.isEmpty(cusCustomerContacts.getDuty())) {

            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "duty");
        }

    }

    @Override
    protected String getMapperNamespace() {
        return "CusCustomerContactsMapper";
    }
}
